import { createRouter, createWebHistory } from 'vue-router';
import { Modal } from 'ant-design-vue';

const routes = [
	//首页
	{
		path: '/',
		redirect: () => {
			const role = localStorage.getItem('role');
			return role === 'admin' ? '/admin-center' : '/home-page';
		}
	},
	{
		path: '/home-page',
		name: 'HomePage',
		component: () => import('@/views/system/HomePage.vue')
	},
	{
		path: '/house-list/:filter?',
		name: 'HouseList',
		component: () => import('@/views/system/HouseList.vue'),
		meta: { title: '房源列表' }
	},
	{
		path: '/house-details/:houseId',
		name: 'HouseDetails',
		component: () => import('@/views/system/HouseDetails.vue'),
		meta: { title: '房源详情' }
	},
	//404
	{
		path: '/:pathMatch(.*)*',
		hidden: true,
		component: () => import('@/views/system/404.vue'),
		meta: { hideNavbar: true }
	},

	//登录页
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/system/SystemLogin.vue')
	},
	//注册页
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/system/SystemRegister.vue')
	},
	//忘记密码
	{
		path: '/forget-pass',
		name: 'forgetPass',
		component: () => import('@/views/system/SystemForgetPass.vue')
	},

	/*
		   用户
	*/
	// 个人信息
	{
		path: '/user-center',
		name: 'UserCenter',
		component: () => import('@/views/user/UserCenter.vue'),
		meta: { requiresAuth: true },
		children: [
			// 修改密码
			{
				path: 'modify-pass',
				name: 'UserModifyPass',
				component: () => import('@/views/user/UserModifyPass.vue'),
				meta: { title: '修改密码', requiresAuth: true }
			},
			// 我的收藏
			{
				path: 'my-favorites',
				name: 'MyFavorites',
				component: () => import('@/views/user/MyFavorites.vue'),
				meta: { title: '我的收藏', requiresAuth: true }
			},
			// 我的预约
			{
				path: 'my-reservation',
				name: 'MyReservation',
				component: () => import('@/views/user/MyReservation.vue'),
				meta: { title: '我的预约', requiresAuth: true }
			}
		]
	},
	{
		path: '/user-chat/:conversationId?',
		name: 'UserChat',
		component: () => import('@/views/user/UserChat.vue'),
		meta: { title: '在线聊天'}
	},

	/*
			 房东
	*/
	{
		path: '/landlord-center',
		name: 'LandlordCenter',
		component: () => import('@/views/landlord/LandlordCenter.vue'),
		meta: { title: '个人中心', requiresAuth: true },
		children: [
			// 修改密码
			{
				path: 'modify-pass',
				name: 'LandlordModifyPass',
				component: () => import('@/views/landlord/LandlordModifyPass.vue'),
				meta: { title: '修改密码', requiresAuth: true }
			},
			// 我的房源
			{
				path: 'my-house',
				name: 'MyHouse',
				component: () => import('@/views/landlord/MyHouse.vue'),
				meta: { title: '我的房源', requiresAuth: true }
			},
			// 我的档期
			{
				path: 'my-schedule',
				name: 'MySchedule',
				component: () => import('@/views/landlord/MySchedule.vue'),
				meta: { title: '我的档期', requiresAuth: true }
			},
			// 上传房源
			{
				path: 'upload-house',
				name: 'UploadHouse',
				component: () => import('@/views/landlord/UploadHouse.vue'),
				meta: { title: '上传房源', requiresAuth: true }
			},
			// 审核预约
			{
				path: 'audit-reservation',
				name: 'AuditReservation',
				component: () => import('@/views/landlord/AuditReservation.vue'),
				meta: { title: '审核预约', requiresAuth: true }
			}
		]
	},
	{
		path: '/landlord-chat/:conversationId?',
		name: 'LandlordChat',
		component: () => import('@/views/landlord/LandlordChat.vue'),
		meta: { title: '在线聊天' , requiresAuth: true}
	},
	/*
		  管理员
   */
	{
		path: '/admin-center',
		name: 'AdminCenter',
		component: () => import('@/views/admin/AdminCenter.vue'),
		meta: { title: '个人中心', requiresAuth: true },
		children: [
			// 修改密码
			{
				path: 'modify-pass',
				name: 'AdminModifyPass',
				component: () => import('@/views/admin/AdminModifyPass.vue'),
				meta: { title: '修改密码', requiresAuth: true }
			},
			// 数据统计分析
			{
				path: 'statistical-data',
				name: 'StatisticalData',
				component: () => import('@/views/admin/StatisticalData.vue'),
				meta: { title: '我的房源', requiresAuth: true }
			},
			// 审核房源
			{
				path: 'audit-house',
				name: 'AuditHouse',
				component: () => import('@/views/admin/AuditHouse.vue'),
				meta: { title: '审核房源', requiresAuth: true }
			},
			// 管理用户
			{
				path: 'manage-user',
				name: 'ManageUser',
				component: () => import('@/views/admin/ManageUser.vue'),
				meta: { title: '管理用户', requiresAuth: true }
			},
			// 管理房东
			{
				path: 'manage-landlord',
				name: 'ManageLandlord',
				component: () => import('@/views/admin/ManageLandlord.vue'),
				meta: { title: '管理房东', requiresAuth: true }
			},
			// 我的审核
			{
				path: 'my-audit',
				name: 'MyAudit',
				component: () => import('@/views/admin/MyAudit.vue'),
				meta: { title: '我的审核', requiresAuth: true }
			},
		]
	}
];

// 标记 Modal 的唯一性，防止重复弹出
let isModalVisible = false;

const router = createRouter({
	history: createWebHistory(),
	routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
	const title = to.meta.title || '花都优居平台'; // 获取当前路由的标题
	document.title = title; // 修改浏览器标签页标题
	next(); // 继续导航

	// 2. 处理根路径重定向（角色判断）
	if (to.path === '/') {
		const role = localStorage.getItem('role');
		const redirectPath = role === 'admin' ? '/admin-center' : '/home-page';
		return next(redirectPath); // 终止后续逻辑
	}

	// 获取 token
	const token = localStorage.getItem('satoken');

	// 检查目标路由是否需要身份认证
	if (to.meta.requiresAuth && !token) {
		if (!isModalVisible) {
			isModalVisible = true; // 标记 Modal 已弹出
			Modal.warning({
				title: '提示',
				content: '您还未登录，请先登录！',
				okText: '去登录',
				onOk: () => {
					isModalVisible = false; // 恢复状态，允许再次弹出 Modal
					router.push('/login');
				},
				onCancel: () => {
					isModalVisible = false; // 恢复状态
					next(false); // 阻止导航
				}
			});
		}
	} else {
		// 如果有 token 或不需要校验，直接放行
		next();
	}
});

export default router;
